package com.feiniu.api.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.feiniu.api.util.IpUtil;

public class IpFilter implements Filter {
    private static final Logger LOGGER = LoggerFactory
            .getLogger(IpFilter.class);

    public void init(FilterConfig arg0) throws ServletException {
        LOGGER.info("*** ip filter init ***");
    }

    public void doFilter(ServletRequest arg0, ServletResponse arg1,
                         FilterChain arg2) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) arg0;
        String ip = IpUtil.getIpAddr(request);
        LOGGER.info("request ip -> {}", ip);
        arg2.doFilter(arg0, arg1);
    }

    public void destroy() {
        LOGGER.info("*** ip filter destroy ***");
    }

}
